package com.advert.common.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.WeekFields;
import java.util.*;

public class DateUtil {
    public static String START = " 00:00:00";
    public static String END = " 23:59:59";
    public static String TIME = ":00";
    public static String FORMAT_DATETIME = "yyyy-MM-dd HH:mm:ss";

    public static DateTimeFormatter formatter = DateTimeFormatter.ofPattern( FORMAT_DATETIME);

    public static DateTimeFormatter formatter_ms = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S");

    public static String new_orderNo(String machindId) {

        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String orderNo = "DD" + dateFormat.format(new Date())+RandomGUIDUtil.getRandom() + RandomGUIDUtil.getLastNum();
        if (orderNo.substring(12,16).equals("0000")) {
            try {
                Thread.sleep(1000l);
            } catch (Exception e) {

            }
            orderNo = new_orderNo(machindId);
        }

        return orderNo;
    }


    public static String getId() {
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        return dateFormat.format(new Date())+RandomGUIDUtil.getRandom() + RandomGUIDUtil.getLastNum();
    }

    public static String new_refundNo() {

        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String orderNo = "TK" + dateFormat.format(new Date())+RandomGUIDUtil.getRandom() + RandomGUIDUtil.getLastNum();
        if (orderNo.substring(12,16).equals("0000")) {
            try {
                Thread.sleep(1000l);
            } catch (Exception e) {

            }
            orderNo = new_refundNo();
        }

        return orderNo;
    }

    public static String shopOrderNo() {

        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String orderNo = "AD" + dateFormat.format(new Date())+RandomGUIDUtil.getRandom() + RandomGUIDUtil.getLastNum();
        return orderNo;
    }

    public static String new_orderNo1() {
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        return dateFormat.format(new Date());
    }

    public static String new_orderNo(String orderNo, String SSS) {
        return "DD" + orderNo + SSS + RandomGUIDUtil.getLastNum();
    }


    public static String date() {
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        return dateFormat.format(new Date());
    }


    public static String date(Date date) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(date);
    }

    public static Date setCurrentMonth(Date date, int i) {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, i);

        return calendar.getTime();
    }

    //当月首日
    public static String setCurrentMonthfirst() {
        Calendar cale = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        cale.set(Calendar.DAY_OF_MONTH, 1);
        return format.format(cale.getTime());
    }

    /**
     * 将Date类型转换成String类型
     *
     * @param date Date对象
     * @return 形如:"yyyy-MM-dd HH:mm:ss"
     */
    public static String date2String(Date date) {
        return date2String(date, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 将Date按格式转化成String
     *
     * @param date    Date对象
     * @param pattern 日期类型
     * @return String
     */
    public static String date2String(Date date, String pattern) {
        if (date == null || pattern == null) {
            return null;
        }
        return new SimpleDateFormat(pattern).format(date);
    }


    public static String date2(Date date) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        return dateFormat.format(date);
    }

    public static String date3(Date date) {
        DateFormat dateFormat = new SimpleDateFormat("HH时mm分ss秒");
        return dateFormat.format(date);
    }

    public static String date4(Date date) {
        DateFormat dateFormat = new SimpleDateFormat("MM-dd");
        return dateFormat.format(date);
    }

    public static String date5(Date date) {
        DateFormat dateFormat = new SimpleDateFormat("MM-dd HH:mm");
        return dateFormat.format(date);
    }

    public static String date6(Date date) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(date);
    }

    public static String date8(Date date) {
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        return dateFormat.format(date);
    }

    public static String date9(Date date) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
        return dateFormat.format(date);
    }

    public static String date10(Date date) {
        DateFormat dateFormat = new SimpleDateFormat("MM.dd");
        return dateFormat.format(date);
    }

    /**
     * 日期格式化：yyyy-MM-dd HH:mm:ss
     *
     * @param date
     * @return
     */
    public static String time(Date date) {
        if (date == null) {
            return null;
        }

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(date);
    }

    public static String time(Date date, String format) {
        DateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(date);
    }

    public static String time2(Date date) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        return date != null ? dateFormat.format(date) : "";
    }

    public static String time3(Date date) {
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        return dateFormat.format(date);
    }

    public static String time4(Date date) {
        DateFormat dateFormat = new SimpleDateFormat("yyMMddHHmmss");
        return dateFormat.format(date);
    }

    public static String time5(Date date) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return dateFormat.format(date);
    }

    public static String time6(Date date) {
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        return dateFormat.format(date);
    }

    public static String timelog() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
        return dateFormat.format(new Date());
    }

    public static Date parsetime(String timeStr) throws ParseException {
        if (StringUtils.isEmpty(timeStr)) {
            return null;
        }
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.parse(timeStr);
    }

    public static Date parsetime(String timeStr, String format) throws ParseException {
        if (StringUtils.isEmpty(timeStr)) {
            return null;
        }
        DateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.parse(timeStr);
    }

    public static Date parsetime1(String datestr) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd-HH:mm:ss");
        return dateFormat.parse(datestr);
    }

    public static Date parsetime3(String timeStr) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        return dateFormat.parse(timeStr);
    }

    public static Date parsetime_start(String timeStr) {
        try {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return dateFormat.parse(timeStr + START);
        } catch (Exception e) {
            return null;
        }
    }

    public static Date parsetime_end(String timeStr) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.parse(timeStr + END);
    }

    public static Date parseDateByDefaultFormat(String timeStr) throws ParseException {
        if (StringUtils.isEmpty(timeStr)) return null;
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.parse(timeStr);
    }


    public static Date parsetime_hour(String timeStr) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.parse(timeStr + TIME);
    }

    public static String wx_time(String timeStr) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        return time(dateFormat.parse(timeStr));
    }

    public static String date2str(Date time) {
        return time != null ? String.valueOf(time.getTime()) : "";
    }


    public static String subdays(Date pDate, Date nDate) {
        long p = pDate.getTime();
        long n = nDate.getTime();
        String sub = String.valueOf((n - p) / 1000L / 60L / 60L / 24L);

        return sub.length() == 1 ? "0" + sub : sub;
    }

    public static String subhours(Date pDate, Date nDate) {
        long p = pDate.getTime();
        long n = nDate.getTime();
        String sub = String.valueOf((n - p) / 1000L / 60L / 60L % 24L);

        return sub.length() == 1 ? "0" + sub : sub;
    }

    public static String subminutes(Date pDate, Date nDate) {
        long p = pDate.getTime();
        long n = nDate.getTime();
        String sub = String.valueOf((n - p) / 1000L / 60L % 60L);

        return sub.length() == 1 ? "0" + sub : sub;
    }

    public static String startTime(Date date) {
        if (date == null) {
            return null;
        }
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd" + START);
        return dateFormat.format(date);
    }

    public static String endTime(Date date) {

        if (date == null) {
            return null;
        }
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd" + END);
        return dateFormat.format(date);
    }

    public static String startPayTime(Date payTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(payTime);
        calendar.add(12, -30);

        return time(calendar.getTime());
    }

    public static String endPayTime(Date payTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(payTime);
        calendar.add(12, 30);

        return time(calendar.getTime());
    }

    public static String startPayTime2(Date updateTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(updateTime);
        calendar.add(12, -2);

        return time(calendar.getTime());
    }

    public static String endPayTime2(Date updateTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(updateTime);
        calendar.add(12, 1);

        return time(calendar.getTime());
    }

    public static String startMonth(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.add(6, -30);

        return startTime(calendar.getTime());
    }

    public static String startWeek(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.add(6, -7);

        return startTime(calendar.getTime());
    }

    public static Date subTime(Date time, int sub) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.add(6, sub);

        return calendar.getTime();
    }


    public static String expireTime(Date time) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(time);
        calendar.add(12, 30);

        return time3(calendar.getTime());
    }

    public static Date onlineTime(Date updateTime) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(updateTime);
        calendar.add(12, -5);

        return calendar.getTime();
    }

    public static Date month_one() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(5, 1);
        calendar.set(11, 0);
        calendar.set(12, 0);
        calendar.set(13, 0);
        return calendar.getTime();
    }

    public static String datesub(String time) {
        return time.substring(0, time.indexOf(" "));
    }

    public static String timesub(String time) {
        return time.substring(0, time.lastIndexOf(":"));
    }

    public static String parsetime2(String timeStr) {
        return timeStr + TIME;
    }

    public static boolean afterDate(String timeStr) {
        boolean afterDate = true;
        try {
            Date time = parsetime(timeStr);

            afterDate = time.after(new Date());
        } catch (ParseException e) {
        }
        return afterDate;
    }

    public static String getTimeByMinute(int minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, minute);
        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
    }


    public static Date str2Date(String time, String format) throws ParseException {
        if (null == format) format = "yyyy-MM-dd HH:mm:ss";
        DateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.parse(time);
    }

    public static Date addOneDay(Date input) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(input);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
    }

    public static String firstDay() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        return format.format(cale.getTime());
    }

    /**
     * 通过时间秒毫秒数判断两个时间的间隔
     *
     * @param start
     * @param end
     * @return
     */
    public static int differentDaysByMillisecond(Date start, Date end) {
        int days = (int) ((end.getTime() - start.getTime()) / (1000 * 3600 * 24));
        return days;
    }

    /**
     * 获取当天0点的日期
     *
     * @param date
     * @return
     */
    public static Date getZeroDate(Date date) throws ParseException {
        return parseDateByDefaultFormat(date(date));
    }


    /**
     * 取得指定日期过 day 天后的日期 (当 day 为负数表示指定日之前);
     *
     * @param date null 表示当天
     * @param day
     * @return
     */
    public static Date nextDay(Date date, int day) {
        Calendar cal = Calendar.getInstance();
        if (date != null) {
            cal.setTime(date);
        }
        cal.add(Calendar.DAY_OF_YEAR, day);
        return cal.getTime();
    }


    /**
     * @return
     */
    public static Date nextMonthFirstDate() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.MONTH, 1);
        return calendar.getTime();
    }

    /**
     * 是否在（包含）上一个月一号零点零时零秒 之后
     */
    public static boolean isAfterLastMonth(LocalDateTime dateTime) {
        LocalDateTime lastMonth = LocalDateTime.now();
        lastMonth = lastMonth.minusMonths(1).with(LocalTime.MAX);
        lastMonth = lastMonth.minusDays(lastMonth.getDayOfMonth());
        return dateTime.isAfter(lastMonth);
    }

    /**
     * 上一个月一号零点零时零秒
     *
     * @return
     */
    public static Date lastMonthFirstDate() {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.add(Calendar.MONTH, -1);
        calendar1.set(Calendar.DAY_OF_MONTH, 1);
        calendar1.set(Calendar.HOUR_OF_DAY, 0);
        calendar1.set(Calendar.MINUTE, 0);
        calendar1.set(Calendar.SECOND, 0);
        return calendar1.getTime();
    }

    public static Date getNow(String format) throws ParseException {
        Date dt = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.parse(dateFormat.format(dt));
    }

    /**
     * 通过年份 和 第几周 获取日期 （该周的第一天和下周的第一天）
     *
     * @param year
     * @param week 从1开始
     * @return
     */
    public static List<Date> getDatesByYearAndWeek(int year, int week) {
        List<Date> res = new ArrayList<>();
        WeekFields weekFields = WeekFields.of(Locale.getDefault());
        LocalDate ld1 = LocalDate.now()
                .withYear(year)
                .with(weekFields.weekOfYear(), week)
                .with(weekFields.dayOfWeek(), 1);

        LocalDate ld2 = LocalDate.now()
                .withYear(year)
                .with(weekFields.weekOfYear(), week + 1)
                .with(weekFields.dayOfWeek(), 1);

        Date dateStart = Date.from(ld1.atStartOfDay(ZoneId.systemDefault()).toInstant());
        Date dateEnd = Date.from(ld2.atStartOfDay(ZoneId.systemDefault()).toInstant());

        res.add(dateStart);
        res.add(dateEnd);


        return res;
    }

    /**
     * 通过年份 和 第几月 获取日期 （该月的第一天和下月的第一天）
     *
     * @param year
     * @param month 从1开始
     * @return
     */
    public static List<Date> getDatesByYearAndMonth(int year, int month) {
        List<Date> res = new ArrayList<>();
        LocalDate ld1 = LocalDate.now().withYear(year).withMonth(month).withDayOfMonth(1);
        LocalDate ld2 = ld1.plusMonths(1L);

        Date dateStart = Date.from(ld1.atStartOfDay(ZoneId.systemDefault()).toInstant());
        Date dateEnd = Date.from(ld2.atStartOfDay(ZoneId.systemDefault()).toInstant());

        res.add(dateStart);
        res.add(dateEnd);


        return res;
    }

    /**
     * 根据开始日期和结束日期 获取日期描述
     *
     * @param start
     * @param end
     * @return （10.01至10.09）
     */
    public static String getDescFromDates(Date start, Date end) {
        DateFormat dateFormat = new SimpleDateFormat("MM.dd");
        String strStart = dateFormat.format(start);
        String strEnd = dateFormat.format(end);
        return String.format("(%s至%s)", strStart, strEnd);
    }


    public static String deliveryCode(String orderNo) {
        String orderNoCode = "";
        String s1 = orderNo.substring(0, 7);
        String s2 = orderNo.substring(7, 14);
        for (int i = 0; i < 7; i++) {
            int n = Integer.parseInt(s1.substring(i, i + 1)) * 2 + 5;
            String n1 = String.valueOf(n).length() == 2 ? String.valueOf(n).substring(1, 2) : String.valueOf(n);

            int m = Integer.parseInt(n1) + Integer.parseInt(s2.substring(i, i + 1));
            String m1 = String.valueOf(m).length() == 2 ? String.valueOf(m).substring(1, 2) : String.valueOf(m);
            orderNoCode = orderNoCode + m1;
        }
        return orderNoCode;
    }

    /**
     * 获取当前日期
     *
     * @return
     */
    public static String getCurrentDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(new Date());
    }

    /**
     * 根据指定格式 获取当前日期
     *
     * @return
     */
    public static String getCurrentDate(String formatDate) {
        SimpleDateFormat format = new SimpleDateFormat(formatDate);
        return format.format(new Date());
    }

    /**
     * 获取两个日期之间的所有日期
     *
     * @param startTime 开始日期
     * @param endTime   结束日期
     * @return
     */
    public static List<String> getDays(String startTime, String endTime) {

        // 返回的日期集合
        List<String> days = new ArrayList<String>();

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date start = dateFormat.parse(startTime);
            Date end = dateFormat.parse(endTime);

            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);

            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
            while (tempStart.before(tempEnd)) {
                days.add(dateFormat.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }

        } catch (ParseException e) {
        }

        return days;
    }

    //指定月份首日
    public static String getFixCurrentMonthfirst(Date date, int i) {
        Calendar cale = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        cale.add(Calendar.MONTH, i);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        return format.format(cale.getTime());
    }

    public static String getYesterday(int day) {
        Calendar cale = Calendar.getInstance();
        cale.add(Calendar.DAY_OF_YEAR, day);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(cale.getTime());
    }

    public static Date getLastMonth(Date date, int month) {
        Instant instant = date.toInstant();//An instantaneous point on the time-line.(时间线上的一个瞬时点。)
        ZoneId zoneId = ZoneId.systemDefault();//A time-zone ID, such as {@code Europe/Paris}.(时区)
        LocalDateTime now = instant.atZone(zoneId).toLocalDateTime();
        now = now.plusMonths(month);
        ZonedDateTime zdt = now.atZone(zoneId);
        return Date.from(zdt.toInstant());
    }

    /**
     * 当月第一天
     *
     * @return
     */
    public static String startTimeMonth() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        //获得实体类
        Calendar ca = Calendar.getInstance();
        //设置最后一天
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMinimum(Calendar.DAY_OF_MONTH));
        //最后一天格式化
        return format.format(ca.getTime());
    }

    /**
     * 当月最后一天
     *
     * @return
     */
    public static String endTimeMonth() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        //获得实体类
        Calendar ca = Calendar.getInstance();
        //设置最后一天
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
        //最后一天格式化
        return format.format(ca.getTime());
    }

    /**
     * 指定月份的最后一天
     *
     * @return
     */
    public static String assignEndTimeMonth(Date input) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(input);
        //设置最后一天
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        //最后一天格式化
        return format.format(calendar.getTime());
    }

    /**
     * 判断日期是否是当月
     *
     * @return
     */
    public static boolean inCurrentMonth(Date givenDate) {
        Date today = new Date();
        return givenDate.getMonth() == today.getMonth() && givenDate.getYear() == today.getYear();
    }

    public static String startTime2(Date date) {
        if (date == null) {
            return null;
        }
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(date);
    }

    /**
     * 根据月份获取其首尾日期
     *
     * @param month 月份（yyyy-MM）
     * @return {"startTime":"yyyy-MM-dd", "endTime":"yyyy-MM-dd"}
     */
    public static Map<String, String> getStartTimeAndEndTime(String month) throws ParseException {
        Map<String, String> map = new HashMap<>(2);

        if (StringUtils.isEmpty(month)) {
            return map;
        }

        Date date = parsetime(month, "yyyy-MM");
        Calendar cale = Calendar.getInstance();
        cale.setTime(date);

        // 获取月份的第一天
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        String startTime = date6(cale.getTime());

        // 获取月份的最后一天
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        String endTime = date6(cale.getTime());

        map.put("startTime", startTime);
        map.put("endTime", endTime);
        return map;
    }

    /**
     * 判断日期是否是今天
     *
     * @param date
     * @return
     */
    public static boolean isToday(Date date) {
        if (date == null) {
            return false;
        }
        String todayStr = date(new Date());
        String dateStr = date(date);

        return todayStr.equals(dateStr);
    }

    /**
     * 判断日期是否在30天内
     *
     * @param date
     * @return
     */
    public static boolean isIn30Days(Date date) {
        if (date == null) {
            return false;
        }

        Date today = new Date();
        date = nextDay(date, 30);

        return date.before(today);
    }

    public static LocalDateTime parseStringToDateTime(String time) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return LocalDateTime.parse(time, df);
    }

    /**
     * localDateTime转Date
     * @param localDateTime
     * @return
     */
    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        return Date.from( localDateTime.atZone( ZoneId.systemDefault()).toInstant());
    }

    /**
     * 根据开始月份获取开始月份的第一天0点0分0秒，
     * 根据结束月份获取结束月份的最后一天23点23分59秒，
     * 月份（yyyy-MM）
     * @param startMonth
     * @param endMonth
     * @return
     * @throws ParseException
     */
    public static Map<String,String> getFirstDateAndLastDate(String startMonth,String endMonth) throws ParseException {
        Map<String, String> firstDate = getStartTimeAndEndTime(startMonth);
        Map<String, String> lastDate = getStartTimeAndEndTime(endMonth);
        Map<String, String> map = new HashMap<>(2);
        if(StringUtils.isEmpty(firstDate.get("startTime"))){
            map.put("firstTime",null);
        }else {
            map.put("firstTime", firstDate.get("startTime") + START);
        }

        if(StringUtils.isEmpty(lastDate.get("endTime"))){
            map.put("lastTime",null);
        }else {
            map.put("lastTime", lastDate.get("endTime") + END);
        }
        return map;
    }

    /**
     * 两日期相差天数
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static long getDays(Date startDate, Date endDate) {
        return ((endDate.getTime() - startDate.getTime()) / (60*60*24*1000));
    }

    /**
     * 获取两个日期之间的所有月份
     *
     * @param startDate
     * @param endDate
     * @return List<yyyy-MM>
     */
    public static List<String> getAllMonth(Date startDate, Date endDate) {
        Calendar calendar = Calendar.getInstance();

        // 获取开始年份和开始月份
        calendar.setTime(startDate);
        int startYear = calendar.get(Calendar.YEAR);
        int startMonth = calendar.get(Calendar.MONTH);

        // 获取结束年份和结束月份
        calendar.setTime(endDate);
        int endYear = calendar.get(Calendar.YEAR);
        int endMonth = calendar.get(Calendar.MONTH);

        // 获取所有月份
        List<String> list = new ArrayList<>();
        for (int i = startYear; i <= endYear; i++) {
            String date = "";
            if (startYear == endYear) {
                for (int j = startMonth; j <= endMonth; j++) {
                    if (j < 9) {
                        date = i + "-0" + (j + 1);
                    } else {
                        date = i + "-" + (j + 1);
                    }
                    list.add(date);
                }
            } else {
                if (i == startYear) {
                    for (int j = startMonth; j < 12; j++) {
                        if (j < 9) {
                            date = i + "-0" + (j + 1);
                        } else {
                            date = i + "-" + (j + 1);
                        }
                        list.add(date);
                    }
                } else if (i == endYear) {
                    for (int j = 0; j <= endMonth; j++) {
                        if (j < 9) {
                            date = i + "-0" + (j + 1);
                        } else {
                            date = i + "-" + (j + 1);
                        }
                        list.add(date);
                    }
                } else {
                    for (int j = 0; j < 12; j++) {
                        if (j < 9) {
                            date = i + "-0" + (j + 1);
                        } else {
                            date = i + "-" + (j + 1);
                        }
                        list.add(date);
                    }
                }
            }
        }

        return list;
    }

    public static String startTimeStr(String date) {
        if (StringUtils.isEmpty(date)) {
            return null;
        }
        return date + START;
    }

    public static String endTimeStr(String date) {
        if (StringUtils.isEmpty(date)) {
            return null;
        }
        return date + END;
    }

    /**
     * 获取多少秒 前/后 的时间
     * @return
     */
    public static String getSecondTime(Date date,boolean bool,int second){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);//设置参数时间 把日期更新SECOND 秒.正数往后推,负数往前移动
        if(bool) {
            calendar.add(Calendar.SECOND, second);
        }else {
            calendar.add(Calendar.SECOND, -second);
        }

        date=calendar.getTime(); //这个时间就是日期往后推一天的结果
        String str = df.format(date);
        return str;
    }

    /**
     * Date转LocalDateTime
     * @param date
     * @return
     */
    public static LocalDateTime dateToLocalDateTime(Date date){
        if(Objects.isNull(date)){
            return null;
        }
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime time = instant.atZone(zoneId).toLocalDateTime();
        return time;
    }

    public static Integer getYear(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.YEAR);
    }

    public static Integer getMonth(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.MONTH) + 1;
    }

    public static Integer getDay(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.DAY_OF_MONTH);
    }

    public static Integer getHour(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.HOUR_OF_DAY);
    }
}
